#
# Makefile for module.
#

CROSS		?= arm-linux-
NAME		:= IIC_GSensor

#
## You shouldn't neet to change anything below this point.
#
AS			:= $(CROSS)gcc -x assembler-with-cpp
CC			:= $(CROSS)gcc
CXX			:= $(CROSS)g++
LD			:= $(CROSS)ld
AR			:= $(CROSS)ar
OC			:= $(CROSS)objcopy
OD			:= $(CROSS)objdump
MKDIR		:= mkdir -p
CP			:= cp -af
RM			:= rm -rf
CD			:= cd
FIND		:= find
TOOLSDIR	:= ../../tools
MKHEADER	:= $(TOOLSDIR)/mk_image/mkv210_image

#
## Load variables of flag.
#
ASFLAGS		:= -g -ggdb -Wall -O2 -nostdlib
CFLAGS		:= -g -ggdb -Wall -O2 -nostdlib -fno-builtin
CXXFLAGS	:= -g -ggdb -Wall -O2 -nostdlib 
LDFLAGS		:= -T source/link.lds -nostdlib
ARFLAGS		:= -rcs
OCFLAGS		:= -v -O binary
ODFLAGS		:= 
MCFLAGS		:= -mcpu=cortex-a8 -march=armv7-a

LIBDIRS		:=
LIBS		:=
INCDIRS		:=
SRCDIRS		:=
OBJDIRS		:=

#
## Add necessary directory.
#
INCDIRS		+= source/include \
			   source/include/libc
LIBDIRS		+= source/lib
SRCDIRS		+= source
OBJDIRS		+= .obj
OUTDIRS		+= output

#
## X variables.
# 
X_ASFALGS	:= $(MCFLAGS) $(ASFLAGS)
X_CFLAGS	:= $(MCFLAGS) $(CFLAGS)
X_CXXFLAGS	:= $(MCFLAGS) $(CXXFLAGS)
X_LDFLAGS	:= $(LDFLAGS)
X_OCFLAGS	:= $(OCFLAGS)
X_LIBDIRS	:= $(LIBDIRS)
X_LIBS		:= $(LIBS) -lgcc

X_OUT		:= $(OUTDIRS)
X_NAME		:= $(patsubst %, $(X_OUT)/%, $(NAME))
X_INCDIRS	:= $(patsubst %, -I %, $(INCDIRS))
X_LIBDIRS	:= $(patsubst %, %, $(LIBDIRS))
X_SRCDIRS	:= $(patsubst %, %, $(SRCDIRS))
X_OBJDIRS	:= $(patsubst %, $(OBJDIRS)/%, $(X_SRCDIRS))

X_LIBFILES	:= $(foreach dir, $(X_LIBDIRS), $(wildcard $(dir)/*.a))
X_SFILES	:= $(foreach dir, $(X_SRCDIRS), $(wildcard $(dir)/*.S))
X_CFILES	:= $(foreach dir, $(X_SRCDIRS), $(wildcard $(dir)/*.c))
X_CPPFILES	:= $(foreach dir, $(X_SRCDIRS), $(wildcard $(dir)/*.cpp))

X_SDEPS		:= $(patsubst %, $(OBJDIRS)/%, $(X_SFILES:.S=.o.d))
X_CDEPS		:= $(patsubst %, $(OBJDIRS)/%, $(X_CFILES:.c=.o.d))
X_CPPDEPS	:= $(patsubst %, $(OBJDIRS)/%, $(X_CPPFILES:.cpp=.o.d))
X_DEPS		:= $(X_SDEPS) $(X_CDEPS) $(X_CPPDEPS)

X_SOBJS		:= $(patsubst %, $(OBJDIRS)/%, $(X_SFILES:.S=.o))
X_COBJS		:= $(patsubst %, $(OBJDIRS)/%, $(X_CFILES:.c=.o))
X_CPPOBJS	:= $(patsubst %, $(OBJDIRS)/%, $(X_CPPFILES:.cpp=.o))
X_OBJS		:= $(X_SOBJS) $(X_COBJS) $(X_CPPOBJS) $(X_LIBFILES)

VPATH		:= $(X_OBJDIRS)

all : $(X_NAME)

$(X_NAME) : $(X_OBJS)
	@echo [LD] Linking $@.elf
	@$(CC) $(X_LDFLAGS) -Wl,--cref,-Map=$@.map $^ -o $@.elf $(X_LIBS)
	@echo [OC] Objcopying $@.bin
	@$(OC) $(X_OCFLAGS) $@.elf $@.bin
	@echo [OD] Objdumping $@
	@$(OD) -D $@.elf > $@_elf.dis
	@echo make header information for irom booting
	@$(MKHEADER) $@.bin

$(X_SOBJS) : $(OBJDIRS)/%.o : %.S
	@echo [AS] $<
	@$(AS) $(X_ASFLAGS) $(X_INCDIRS) -c $< -o $@
	@$(AS) $(X_ASFLAGS) -MD -MP -MF $@.d $(X_INCDIRS) -c $< -o $@

$(X_COBJS) : $(OBJDIRS)/%.o : %.c
	@echo [CC] $<
	@$(CC) $(X_CFLAGS) $(X_INCDIRS) -c $< -o $@
	@$(CC) $(X_CFLAGS) -MD -MP -MF $@.d $(X_INCDIRS) -c $< -o $@

$(X_CPPOBJS) : $(OBJDIRS)/%.o : %.cpp
	@echo [CXX] $<
	@$(CXX) $(X_CXXFLAGS) $(X_INCDIRS) -c $< -o $@
	@$(CXX) $(X_CXXFLAGS) -MD -MP -MF $@.d $(X_INCDIRS) -c $< -o $@

tools:
	@$(MAKE) -C $(TOOLSDIR)/mk_image

lib:
	@$(MAKE) -C source/lib all

tools_clean:
	@$(MAKE) -C $(TOOLSDIR)/mk_image clean

lib_clean:
	@$(MAKE) -C source/lib clean

src_clean:
	@$(RM) $(OBJDIRS) $(X_OBJDIRS) $(X_OUT) $(X_DEPS) 


clean: lib_clean src_clean
	@echo "clean all."

.PHONY: all clean test tools lib tools_clean lib_clean src_clean
	
#
## Include the dependency files, should be place the last of makefile
#
sinclude $(shell $(MKDIR) $(X_OBJDIRS) $(X_OUT)) $(X_DEPS)
